/*
 *****************************************************************************
 * Copyright 2004-2012 Aiti Ltd. All rights reserved.
 *****************************************************************************
 *  Change Activity:
 *  --------------------------------------------------------------------------
 *  Date      Author          Description
 *  --------------------------------------------------------------------------
 *  Jan 14, 2013 ThinhNV2  Initial Code drop.
 *****************************************************************************
 */
package com.aiti.c1202m.core.logger;

import java.util.ResourceBundle;

/**
 * <H3>
 * 	Base LoggerFactory.
 * </H3>
 * 
 * @author 
 * @since 2004/10/22
 * @see IfLogger
 */
public abstract class AbstractLoggerFactory {
	private static final String PROPERTY_FILE = "com.aiti.base.properties.logger";
	private static final String PROPERTY_KEY_FACTORY_CLASS = "factory.class";
	private static AbstractLoggerFactory factory = null;
	private static final String DEFAULT_LOGGER_FACTORY_CLASS = "com.aiti.core.logger.logging.LoggingLoggerFactory";

	//	***** Constructor *****

	//	***** Public method *****

	public abstract IfLogger getLogger(String className);

	/**
	 * return LoggerFactory instance.
	 * 
	 * read concrete class from "com/aiti/base/properties/logger.properties" file.
	 *
	 * "logger.properties"
	 * <code>
	 * 	factory.class=com.nsk.base.mid.java.common.util.trace.log4j.Log4jLoggerFactory
	 * </code>
	 *
	 * @return AbstractLoggerFactory
	 * @throws IllegalStateException - cause when property configuration is not correct.
	 */
	public static AbstractLoggerFactory getLoggerFactory() {
		if (factory != null) {
			return factory;
		} 

		ResourceBundle _bundle = ResourceBundle.getBundle(PROPERTY_FILE);
		String _factoryClass = _bundle.getString(PROPERTY_KEY_FACTORY_CLASS);
		if (_factoryClass == null) {
			_factoryClass = DEFAULT_LOGGER_FACTORY_CLASS;
		}

		try {
			factory = (AbstractLoggerFactory)Class.forName(_factoryClass).newInstance();
			return factory;
		} catch (InstantiationException e) {
			throw new IllegalStateException(_factoryClass + " can not instantiate.\noriginal message:" + e.getMessage());
		} catch (IllegalAccessException e) {
			throw new IllegalStateException(_factoryClass + " can not access.\noriginal message:" + e.getMessage());
		} catch (ClassNotFoundException e) {
			throw new IllegalStateException(_factoryClass + " is not found.\noriginal message:" + e.getMessage());
		}
	}

	//	***** Protected method *****

	//	***** Private method *****

	//	***** Call back methods *****

	//	***** Getter and Setter *****

}
